#include "stdafx.h"
#include "Box.h"
#include "PositionColored.h"


CBox::CBox()
{
	this->m_vb = NULL;
	this->m_ib = NULL;
}


CBox::~CBox()
{
}

bool CBox::CreateVertex(LPDIRECT3DDEVICE9   g_pd3dDevice)
{
	g_pd3dDevice->CreateVertexBuffer(
		8 * sizeof(PositionColored),
		D3DUSAGE_WRITEONLY,
		D3DFVF_POSITIONCOLORED,
		D3DPOOL_MANAGED,
		&m_vb,
		0);

	g_pd3dDevice->CreateIndexBuffer(
		36 * sizeof(WORD),
		D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16,
		D3DPOOL_MANAGED,
		&m_ib,
		0);

	// define unique vertices:
	PositionColored* vertices;
	m_vb->Lock(0, 0, (void**)&vertices, 0);

	// vertices of a unit cube
	vertices[0] = PositionColored(-1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(255, 0, 0));
	vertices[1] = PositionColored(-1.0f, 1.0f, -1.0f, D3DCOLOR_XRGB(0, 255, 0));
	vertices[2] = PositionColored(1.0f, 1.0f, -1.0f, D3DCOLOR_XRGB(255, 0, 0));
	vertices[3] = PositionColored(1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(255, 0, 0));
	vertices[4] = PositionColored(-1.0f, -1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0));
	vertices[5] = PositionColored(-1.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0));
	vertices[6] = PositionColored(1.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0));
	vertices[7] = PositionColored(1.0f, -1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0));

	m_vb->Unlock();

	// define the triangles of the cube:
	WORD* indices = 0;
	m_ib->Lock(0, 0, (void**)&indices, 0);

	// front side
	indices[0] = 0; indices[1] = 1; indices[2] = 2;
	indices[3] = 0; indices[4] = 2; indices[5] = 3;

	// back side
	indices[6] = 4; indices[7] = 6; indices[8] = 5;
	indices[9] = 4; indices[10] = 7; indices[11] = 6;

	// left side
	indices[12] = 4; indices[13] = 5; indices[14] = 1;
	indices[15] = 4; indices[16] = 1; indices[17] = 0;

	// right side
	indices[18] = 3; indices[19] = 2; indices[20] = 6;
	indices[21] = 3; indices[22] = 6; indices[23] = 7;

	// top
	indices[24] = 1; indices[25] = 5; indices[26] = 6;
	indices[27] = 1; indices[28] = 6; indices[29] = 2;

	// bottom
	indices[30] = 4; indices[31] = 0; indices[32] = 3;
	indices[33] = 4; indices[34] = 3; indices[35] = 7;

	m_ib->Unlock();


	return true;
}

bool CBox::Initialize(LPDIRECT3DDEVICE9 g_pd3dDevice)
{
	if (isOn &!isInitialize)
	{
		CreateVertex(g_pd3dDevice);
		isInitialize = true;
	}
	return true;
}

void CBox::Update(LPDIRECT3DDEVICE9 g_pd3dDevice)
{
	if (isOn & !isInitialize)
	{
		Initialize(g_pd3dDevice);
	}
}

void CBox::Render(LPDIRECT3DDEVICE9 g_pd3dDevice)
{
	if (isOn && isInitialize)
	{
		D3DXMATRIX rx, ry;
		// rotate 45 degrees on x-axis
		//D3DXMatrixRotationX(&rx, 3.14f / 4.0f);
		// incremement y-rotation angle each frame
		//D3DXMatrixRotationY(&ry, m_yRotAngle);
		//m_yRotAngle += 0.1f;

		// reset angle to zero when angle reaches 2*PI
		/*if (m_yRotAngle >= 6.28f) {
			m_yRotAngle = 0.0f;
			}*/


		g_pd3dDevice->SetStreamSource(0, m_vb, 0, sizeof(PositionColored));
		g_pd3dDevice->SetIndices(m_ib);
		g_pd3dDevice->SetFVF(D3DFVF_POSITIONCOLORED);

		D3DXMATRIX rx1, ry1;
		g_pd3dDevice->GetTransform(D3DTS_WORLD, &rx1);
		D3DXMatrixScaling(&ry1, 5, 5, 5);
		D3DXMATRIX p = ry1 * rx1;
		g_pd3dDevice->SetTransform(D3DTS_WORLD, &p);

		// Draw cube.
		g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
		g_pd3dDevice->SetTransform(D3DTS_WORLD, &rx1);
	}
}
